package shapes.shapes2D.curves
{
	
	import flash.display.Graphics;
	import flash.geom.Point;
	import shapes.shapes2D.curves.CubicPath;
	import shapes.shapes2D.Line;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class PolyLine
	{
		private var _lines:Vector.<Line>;
		private var _points:Vector.<Point>;
		
		public function PolyLine() 
		{
			lines = new Vector.<Line>();
			points = new Vector.<Point>();
		}
		
		
		public function draw( graphics:Graphics ):void
		{
			
			var p:Point;
			graphics.moveTo( points[ 0 ].x, points[ 0 ].y );
			for each( p in lines )
			{
				graphics.lineTo( points[ 0 ].x, points[ 0 ].y );
			}
			
		}
		
		public function drawCubic( graphics:Graphics, precision:Number = 0.01, loop:Boolean = false ):void
		{
			
			
			CubicPath.precision = precision;
			var curve:Vector.<Point> = CubicPath.compute( points, loop );
			curve.reverse();
			
			var p:Point;
			graphics.moveTo( curve[ 0 ].x, curve[ 0 ].y );
			for each( p in curve )
			{
				graphics.lineTo( p.x, p.y );
			}
			for each( p in points )
			{
				graphics.lineTo( p.x, p.y );
			}
			graphics.lineTo( curve[ 0 ].x, curve[ 0 ].y );
			
		}
		
		public function addPoint( p:Point ):void
		{
			points.push( p );
		}
		
		public function get points():Vector.<Point> { return _points; }
		
		public function set points(value:Vector.<Point>):void 
		{
			_points = value;
			resetLines();
		}
		
		
	}

}